package servlet;

import interfaceDAO.IDAOPreference;
import interfaceDAO.IDAOUser;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.DAOFactory;
import domain.Preference;
import domain.User;

/**
 * Servlet implementation class RajouterPreference
 */
@WebServlet("/RajouterPreference")
public class RajouterPreference extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RajouterPreference() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("RajouterPreference.doGet()");

		String nomUser = request.getParameter("nomUser");
		String preference = request.getParameter("preference");
		String adressePreference = request.getParameter("adressePreference");
		/* Preference addPreference(User user, String alias, String address) */
		IDAOUser daoUser = DAOFactory.getDAOUser();
		User user = daoUser.getUser(nomUser);

		IDAOPreference daoPreference = DAOFactory.getDAOPreference();
		Preference adresse = daoPreference.addPreference(user, preference, adressePreference);
		// user.getAddressBook().add(adresse);
		user = daoUser.getUser(nomUser); 

		System.out.println("RajouterPreference.doGet()::: Adding preference ["
				+ adresse + "]");
		System.out.println("Nouvelles preferences de " + user.getPseudo() + " :::: ");
		for (Preference pref : user.getAddressBook()) {
			System.out.println(" ==> " + pref);
		}
		System.out.println("========================================================");

		HttpSession userSession = request.getSession();
		userSession.setAttribute(IConstants.user, user);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("RajouterPreference.doPost()");
		doGet(request, response);
	}

}
